<template>
  <div class="login">
    <div>
      <img src="../assets/images/login_img.png"/>
    </div>
    <div style="display: flex;flex-direction: column;align-items: center;">
      <img src="../assets/logo/logo1.png" style="width: 264px;margin-bottom: 58px;"/>
      <div class="login-form">
        <div class="login-right ">
          <div class="custom-tab">
            <div @click="changeTab(0)" :class="login_type == 0?'tab-item choose':'tab-item'">
              <span>密码登录</span>
            </div>
            <div @click="changeTab(1)" :class="login_type == 1?'tab-item choose':'tab-item'">
              <span>短信快捷登录</span>
            </div>
          </div>
          <el-form v-if="login_type == 0" ref="loginForm" :model="loginForm" :rules="loginRules">
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-form-item prop="password" :rules="[{ required: true, trigger: 'blur', message: '请输入您的密码' }]">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </el-form-item>
<!--            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
            <el-form-item style="width:100%;" prop="userType">
              <el-radio-group v-model="userType">
                <el-radio :label="1">平台用户</el-radio>
                <el-radio :label="2">货主</el-radio>
                <el-radio :label="3">档口</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                type="primary"
                style="width:100%;height: 50px;background: linear-gradient(180deg, #02C08B 0%, #00B582 99%);"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right;" v-if="register">
                还没有账户，<router-link class="link-type text-navy" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
          </el-form>
          <el-form v-if="login_type == 1" ref="loginForm" :model="codeLoginForm" :rules="loginRules">
            <el-form-item prop="username">
              <el-input
                v-model="codeLoginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" :rules="[{ required: true, trigger: 'blur', message: '请输入验证码' }]">
              <el-input
                v-model="codeLoginForm.code"
                auto-complete="off"
                placeholder="验证码"
                @keyup.enter.native="handleLogin"
              >
                  <el-button slot="append" @click="getSms" :disabled="!canSendCode">
                    {{ canSendCode ? '发送验证码' : `${countdown}秒后重试` }}
                  </el-button>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="codeLoginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </el-form-item>
            <!--            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
            <el-form-item style="width:100%;" prop="userType">
              <el-radio-group v-model="userType">
                <el-radio :label="1">平台用户</el-radio>
                <el-radio :label="2">货主</el-radio>
                <el-radio :label="3">档口</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                type="primary"
                style="width:100%;height: 50px;background: linear-gradient(180deg, #02C08B 0%, #00B582 99%);"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right;" class="" v-if="register">
                还没有账户，<router-link class="link-type text-navy" :to="'/register'">立即注册</router-link>

              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, xnfLoginPhone, getSmsCode, getSmsCaptcha } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      table_height:window.innerHeight - 300,
      userType:1,
      codeUrl: '',
      loginForm: {
        username: '',
        password: '',
        code: '',
        uuid: '',
        userType:1
      },
      codeLoginForm: {
        username: '',
        password: '',
        code: '',
        uuid: '',
        userType:2
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
      },
      login_type: 0,
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined,
      // 验证码倒计时
      countdown: 60,
      timer: null,
      canSendCode: true
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
          this.codeLoginForm.uuid = res.uuid
        }
      })
    },
    getSms(){
      if (!this.canSendCode) return;
      if (this.codeLoginForm.username == ''){
        this.$message.error('请输入手机号码')
        return false
      }
      getSmsCode(this.codeLoginForm.username,'login').then(res => {
        this.$message.success(res.msg)
        this.codeLoginForm.uuid = res.data.uuid;
        // this.registerForm.code = res.data.code
        this.startCountdown();
      })
    },
    startCountdown() {
      this.canSendCode = false;
      this.countdown = 60;
      this.timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--;
        } else {
          this.canSendCode = true;
          clearInterval(this.timer);
          this.timer = null;
        }
      }, 1000);
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            if (this.loginForm.rememberMe) {
              Cookies.set('username', this.loginForm.username, { expires: 30 })
              Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
              Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
            } else {
              Cookies.remove('username')
              Cookies.remove('password')
              Cookies.remove('rememberMe')
            }
            if (this.login_type == 0){
              let formData = {
                ...this.loginForm,
                userType: this.userType
              };
              this.$store.dispatch('Login', formData).then(() => {
                this.$router.push({ path: this.redirect || '/' }).catch(() => {})
                // this.$router.push({ path:'supply/list/order' }).catch(() => {})
              }).catch(() => {
                this.loading = false
                if (this.captchaEnabled) {
                  this.getCode()
                }
              })
            }else{
              let formData = {
                ...this.codeLoginForm,
                userType: this.userType
              };
              console.log(formData)
              this.$store.dispatch('PhoneLogin', formData).then(() => {
                // this.$router.push({ path: this.redirect || '/' }).catch(() => {
                // })
                this.$router.push({ path:'supply/list/order' }).catch(() => {})
              }).catch(() => {
                this.loading = false
                if (this.captchaEnabled) {
                  this.getCode()
                }
              })
            }
          }
        })

    },
    changeTab(val) {
      this.login_type = val
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
@import "../assets/styles/element-variables.scss";

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");
  //background-size: cover;
  background: linear-gradient(180deg, #D3EEEA 0%, #F4FAFA 100%);

  &-right {
    display: flex;
    flex-direction: column;
    align-items: center;

    img {
      margin-bottom: 24px;
    }
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.custom-tab {
  display: flex;
  margin-bottom: 28px;
  width: 100%;

  .tab-item {
    flex: 1;
    text-align: center;
    cursor: pointer;
    display: flex;
    justify-content: center;
    height: 26px;
    font-size: 18px;

    &.choose {
      span {
        color: $--color-primary;
        font-weight: bold;
        border-bottom: 2px solid $--color-primary;
      }
    }
  }
}

.login-form {
  border-radius: 6px;
  background: linear-gradient(180deg, #FFFFFF 0%, rgba(255, 255, 255, 0.9) 100%);

  box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.05);
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
